/**
 * Created by zycool on 2016/7/26.
 * 统计 - 缺陷维修管理
 */


;(function($,window,document,undefined){
    // ui工程路径
    var _resource_path = $('#JQ_resource_path').attr('content');

    // 缺陷维修ID
    var taskid = $('#JQ_taskid').val();
    var url = DyyBase.root_path+'/pbf/defectRepairMgr/defectStatistics/'+taskid+'.json';
    DyyBase.ajax(url,{},function(dataset){
        if(dataset.bool){
            if(dataset.result){
                var result = DyyBase.clearNull(dataset.result);
                for(var o in result){
                    $('#'+o).text(result[o]).prop('title',result[o]);
                }
            }

            var defectInfo = dataset.result.defectInfoStatisticsList;
            var repairStateList = dataset.result.repairStateStatisticsList;

            var p = '';
            for(var j = 0, len = defectInfo.length; j < len; j++){
                var total = parseInt(defectInfo[j].alreadyRepair) + parseInt(defectInfo[j].waitForRepair) + parseInt(defectInfo[j].maintenance) + parseInt(defectInfo[j].difficultToRepair) + parseInt(defectInfo[j].waitForReplace);
                p += '<p>'
                    +'<span style="width: 16%">'+defectInfo[j].featureType+'</span>'
                    +'<span style="width: 14%">'+total+'</span>'
                    +'<span style="width: 14%">'+defectInfo[j].alreadyRepair+'</span>'
                    +'<span style="width: 14%">'+defectInfo[j].waitForRepair+'</span>'
                    +'<span style="width: 14%">'+defectInfo[j].maintenance+'</span>'
                    +'<span style="width: 14%">'+defectInfo[j].difficultToRepair+'</span>'
                    +'<span style="width: 14%">'+defectInfo[j].waitForReplace+'</span>'
                    +'</p>';
            }
            $('#JQ_statics_body').html(p);

            if (repairStateList.length > 0){
                var list = d3Charts.formart(repairStateList,{x:'name',y:'val'});
                total = 0;
                for(var x = 0, lenx = list.length; x < lenx; x++){
                    total += parseInt(list[x].y);
                }
                list.unshift({x:'default',y:0});
                list.push({x:'default',y:0});
                d3Charts.bar({
                    container: 'JQ_defect',
                    color: ['#61ade9'],
                    data: list,
                    xAxis: {
                        data: [],
                        ticks: 0,
                        show: true,
                        render: function(d){
                            if(d === 'default'){
                                return '';
                            }else{
                                return d;
                            }
                        }
                    },
                    yAxis: {
                        data: [0,total],
                        ticks: total,
                        show: true,
                        render: function(d){
                            return d;
                        }
                    },
                    layout: {
                        xtag: '',
                        ytag: '维修数量',
                        margin: {
                            left: 30,
                            right: 30,
                            top: 30,
                            bottom: 30
                        },
                        xgrid: true,
                        ygrid: true
                    },
                    tip: {
                        show: true,
                        xtag: '',
                        ytag: '数量'
                    }
                });
            } else {
                $('#JQ_defect').trigger('clear');
            }

        }
    },{type:'GET'});

    // 返回
    $('#JQ_statistics_goback').click(function(){
        window.location.href = DyyBase.root_path + '/pbf/RepairTask/init.html';
    });

    // 导出
    $('#JQ_statistics_export').click(function(){
        var $this = $(this);
        DyyBase.isLogin(function(){
            var pipeName = $('#pipeLineName').text();
            var projectName = $('#repairTaskName').text();
            $this.EXPT({
                type: 'name',
                name: pipeName + projectName + '缺陷维修统计图',
                yesBtnAct: function(callback,fname){
                    callback();
                    canvasPdf.exportHtmlPdf({
                        dom: $('#JQ_pdf_area'),
                        server: DyyBase.root_path + '/comm/downImgPdf.json',
                        param: {
                            fileName: fname,
                            name: projectName + '缺陷维修统计图'
                        }
                    });
                }
            });
        });
    });

})(jQuery,window,document);
